adminPanel.controller('StudentSocialBehaviourEditableGrid',['$scope', '$filter', '$http', '$q','cacheFactory', function($scope, $filter, $http, $q,cacheFactory) {
	
	$q;
	$scope.socialBehaviourList=cacheFactory.getSocialBehaviourList();
	$scope.gradeList=cacheFactory.getGradeList();
	$scope.termList=cacheFactory.getTermList();
	$scope.gradePoints=cacheFactory.getGradePoints();
	$scope.showGrid=false;
	
	$scope.searchCriteria.recordType='SOCIAL_BHEAVIOUR';
	//use to send data to server
	var studentRecord={"socialBehaviourActivities":null, "searchCriteria":null};
	
	//get the loaded into system
	$scope.socialBehaviourVos =[];
	$scope.recommendations=[];
	$scope.showRecommendation=false;
	$scope.handleSavedRecommendation=function(data){
		if(data.type=='SAVE_RECOMMENDATION'){
			$scope.getRecommendations();
		}
	}
	$scope.handleDeletedRecommendation=function(data){
		if(data.type=='DELETE_RECOMMENDATION'){
			$scope.getRecommendations();
		}
	}
	
	
	$scope.getRecommendations=function(){
	    var httpResult=$http.post(BASE_URL+'admin/handleRequest/studentRecord/RECOMMENDATION_GET', {searchCriteria:$scope.searchCriteria});
	    httpResult.success(function(data){
	    	if(data.status=='SUCCESS'){
	    		$scope.recommendations=data.result;
	    		$scope.showRecommendation=true;
	    	}else{
	    		$scope.showRecommendation=false;
	    	}
	    });
	}
	//register event
	  RegisterEventHandler("StudentSocialBehaviourEditableGrid2",$scope.handleDeletedRecommendation);
	  RegisterEventHandler("StudentSocialBehaviourEditableGrid1",$scope.handleSavedRecommendation);	
	$scope.getDetails= function(){                 
		studentRecord.searchCriteria= $scope.searchCriteria;
		$http.post(BASE_URL+"admin/handleRequest/studentRecord/GET_SOCIAL_BEHAVIOURS_BY_GRADE_AND_TERM/",studentRecord).success(function(data){
			if(data.status=='SUCCESS'){
				$scope.socialBehaviourVos=data.result;	
				$scope.showGrid=true;
			}else if(data.status=='NOT_FOUND'){
				alert("No record found for this entity.");
				$scope.socialBehaviourVos=[];
				$scope.showGrid=true;
			}else{
				alert(data.status +":"+data.msg);
			}
			$scope.getRecommendations();
		}).error(function(){
			ERROR_LOGGER("Server Error");
		});
	};
	
	$scope.criteriaChanged=function(){
		$scope.showGrid=false;
		$scope.socialBehaviourVos=[];
	};
	
	$scope.showSocialBehaviour = function(socialBehaviourVo) {
	    var selected = [];
	    if(socialBehaviourVo.socialBehaviourId) {
	      selected = $filter('filter')($scope.socialBehaviourList, {id: socialBehaviourVo.socialBehaviourId});
	    }
	    return selected.length ? selected[0].behaviour : 'Not set';
	};

	$scope.showGradePoint = function(socialBehaviourVo) {
	    var selected = [];
	    if(socialBehaviourVo.gradePointId) {
	      selected = $filter('filter')($scope.gradePoints, {id: socialBehaviourVo.gradePointId});
	    }
	    return selected.length ? selected[0].value : 'Not Set';
	};
	
	  $scope.checkValue = function(data, id) {
		  //validation will go here
	  };

	  // filter socialBehaviourVo to show
	  $scope.filterSocialBehaviours = function(socialBehaviourVo) {
		return socialBehaviourVo.rowDeleted!== true;
	  };

	  // mark user as deleted
	  $scope.deleteSocialBehavRow = function(cid,sid) {
		
	    var filtered = $filter('filter')($scope.socialBehaviourVos, {sid: sid});
	    if (filtered.length) {
	    	filtered[0].rowDeleted = true;
	    	$scope.socialBehaviourVos[sid].rowDeleted=true;
	    }
	  };

	  // add user
	  $scope.addSocialBehavRow = function() {
	    $scope.socialBehaviourVos.push({
	      cid: $scope.socialBehaviourVos.length+1,
	      sid: 0,
	      socialBehaviourId: null,
	      gradePointId:null,
	      remarks: null,
	      newRow:true,
	    });
	  };

	  // cancel all changes
	  $scope.cancel = function() {
	    for (var i = $scope.socialBehaviourVos.length; i--;) {
	      var vo = $scope.socialBehaviourVos[i];    
	      // undelete
	      if (vo.isDeleted) {
	    	  $scope.socialBehaviourVos[i].rowDeleted=false;
	        delete vo.isDeleted;
	      }
	      // remove new 
	      if (vo.newRow) {
	        $scope.socialBehaviourVos.splice(i, 1);
	      }
	    };
	  };

	  // save edits
	  $scope.saveTable = function() {
	    var results = [];
	    for (var i = $scope.socialBehaviourVos.length; i--;) {
	      var vo = $scope.socialBehaviourVos[i];
	      // actually delete user
	      if (vo.rowDeleted) {
	        //$scope.socialBehaviourVos.splice(i, 1);
	      }
	      // mark as not new 
	      if (vo.newRow) {
	    	  vo.newRow = true;
	      }
	      vo.termId=$scope.searchCriteria.termId;
	      vo.gradeId=$scope.searchCriteria.gradeId;
	    }
	    //Create student vo object
	    studentRecord.socialBehaviourActivities=$scope.socialBehaviourVos;
	    studentRecord.searchCriteria= $scope.searchCriteria;
	    // send on server
	    var httpResult=$http.post(BASE_URL+'admin/handleRequest/studentRecord/SAVE_SOCIAL_BEHAVIOURS', studentRecord);
	    httpResult.success(function(data){
	    		alert(data.status);
	    });
	    results.push(httpResult);
	    return $q.all(results);
	  };
	}]);